package com.luxsoft.sw3.gasoc.model;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.luxsoft.siipap.model.Direccion;
import com.luxsoft.siipap.model.core.Cliente;
import com.luxsoft.siipap.service.ServiceLocator2;
import com.luxsoft.siipap.util.DateUtil;

public class ExportadorACentauri {
	
	
	
	public static String exportar(Date fecha) throws Exception{
		List<Socio> socios=ServiceLocator2.getHibernateTemplate().find("from Socio s where date(s.log.creado)=?",fecha);
		return exportar(socios,new File("/toCentaur.csv"));
	}
	
	public static String  exportar(Socio socio)throws Exception{
		File file=new File("/"+socio.getId()+"_toCentaury.csv");
		List<Socio> socios=new ArrayList<Socio>();
		socios.add(socio);
		exportar(socios,file);
		return "Socio exportado en: "+file.getAbsolutePath();
	}
	
	protected static String exportar(List<Socio> socios,File file)throws Exception{
		System.out.println("Socios a exportar: "+socios.size() +" Destino: "+file.getAbsolutePath());
		FileOutputStream fout=new FileOutputStream(file,false);
		OutputStreamWriter out = new OutputStreamWriter(fout,"UTF-8");
		PrintWriter writer=new PrintWriter(out);
		BufferedWriter buf=new BufferedWriter(writer);
		char separador=',';
		// Columnas
		buf.write("FirstName");
		buf.write(separador);
		buf.write("LastName");
		buf.write(separador);
		buf.write("Address");
		buf.write(separador);
		buf.write("City");
		buf.write(separador);
		buf.write("ZIP");
		buf.write(separador);
		buf.write("State");
		buf.write(separador);
		buf.write("Country");
		buf.write(separador);
		buf.write("Phone1");
		buf.write(separador);
		buf.write("Phone2");
		buf.write(separador);
		buf.write("EMail1");
		buf.write(separador);
		buf.newLine();
		
		for(Socio s:socios){
			
			buf.write(s.getNombres());//FirstName 
			buf.write(separador);
			buf.write(s.getApellidoP());//"LastName");
			buf.write(separador);
			Direccion dir=s.getCliente().getDireccionFiscal();
			String pattern="{0} {1} {2}";
			buf.write(MessageFormat.format(pattern
					, dir.getCalle()
					,dir.getNumero()
					,dir.getColonia()
					));
			buf.write(separador);			
			buf.write(dir.getMunicipio());
			buf.write(separador);
			buf.write(dir.getCp());
			buf.write(separador);
			buf.write(dir.getEstado());
			buf.write(separador);
			buf.write("Mexico");
			buf.write(separador);
			buf.write(StringUtils.stripToEmpty(s.getTelefono1()));
			buf.write(separador);
			buf.write(StringUtils.stripToEmpty(s.getTelefono2()));
			buf.write(separador);
			buf.write(StringUtils.stripToEmpty(s.getEmail()));
			buf.write(separador);
			buf.newLine();
		}
		
		buf.close();		
		writer.close();
		out.close();
		/*
		FirstName	50 characters
		LastName	50 characters		
		Address		50 characters
		City		50 characters
		ZIP			50 characters
		State		50 characters
		Country		50 characters
		Phone1		50 characters
		Phone2		50 characters
		Phone3		50 characters
		EMail1		50 characters
		//EMail2		50 characters
		//EMail3		50 characters
		//Initials		50 characters
		 * 
		 */
		return file.getAbsolutePath();
	}
	
	public static void main(String[] args)throws Exception {
		
		exportar(DateUtil.toDate("7/02/2012"));
	}

}
